﻿//-----------------------------------------------------------------------
// <copyright file="HttpGet.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace VisualStudioOnlineOData
{
    using System;
    using System.Net;
    using System.Net.Http;
    using System.Threading.Tasks;

    /// <summary>
    ///
    /// </summary>
    public static class Http
    {
        /// <summary>
        /// Gets the asynchronous.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="credentials">The credentials.</param>
        /// <returns></returns>
        public static async Task<string> GetAsync(Uri uri, Credentials credentials)
        {
            using (var handler = new HttpClientHandler())
            {
                handler.Credentials = new NetworkCredential(credentials.Account + "\\" + credentials.Username, credentials.Password);
                using (var client = new HttpClient(handler))
                {
                    try
                    {
                        return await client.GetStringAsync(uri);
                    }
                    catch (System.Net.Http.HttpRequestException)
                    {
                        return null;
                    }
                }
            }
        }
    }
}